package com.example.demo01.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.demo01.R;
import com.example.demo01.entity.UserDemo;

import java.util.List;

public class GridDemoAdapter extends BaseAdapter {

    private Context context;
    private List<UserDemo> list;

    public GridDemoAdapter(Context context, List<UserDemo> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public UserDemo getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return list.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder; //. ViewHolder模式优化（避免重复查找视图）
        if (convertView == null) {
            convertView = LayoutInflater
                    .from(context)
                    .inflate(R.layout.item_grid_test01, parent, false); //. 根据布局文件创建视图
            holder = new ViewHolder();
            holder.tv_name = convertView.findViewById(R.id.tv_name); //. 初始化视图组件
            holder.tv_nickname = convertView.findViewById(R.id.tv_nickname); //. 初始化视图组件
            convertView.setTag(holder); //. 将ViewHolder绑定到视图
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tv_name.setText(getItem(position).getName()); //. 设置视图组件数据
        holder.tv_nickname.setText(getItem(position).getNickname()); //. 设置视图组件数据

        return convertView;
    }

    /// 静态内部类
    static class ViewHolder {
        TextView tv_name; //* 姓名
        TextView tv_nickname; //* 昵称
    }
}
